How do I add a click handler to a new element when I create it? [closed]

Posted by vicky on Stack Overflow See other posts from Stack Overflow or by vicky
Published on 2010-03-30T14:10:11Z Indexed on 2010/03/30 14:23 UTC
Read the original article Hit count: 412

Filed under:
|

How do I add a click handler to a new element when I create it? This is what I have tried, but it does not work as expected:

DeCheBX = $('MyDiv').insert(new Element('input', {
    'type': 'checkbox',
    'id': "Img" + obj[i].Nam,
    'value': obj[i].IM,
    'onClick': SayHi(this)
}));
document.body.appendChild(DeCheBX);
DeImg = $('MyDiv').insert(new Element('img', {
    'id': "Imgx" + obj[i].Nam,
    'src': obj[i].IM
}));
document.body.appendChild(DeImg);
}
SayHi = function (x) {
    try {

        if ($(x).checked == true) {
            alert("press" + x);
        }

    }
    catch (e) {
        alert("error");

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about prototype